﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EpcTagLibrary
{
    public class SSCC : EPC
    {
        private int m_Filter;
        private int m_Partition;
        private string m_CompanyPrefix;
        private string m_SerialReference;

        public SSCC(Bits bits)
            : base(bits)
        {
            Divide();
        }

        public SSCC(byte[] bytes)
            : base(bytes)
        {
            Divide();
        }

        private void Divide()
        {
            m_Filter = (int)Common.BitsToInt(GetFieldBits(1));
            m_Partition = (int)Common.BitsToInt(GetFieldBits(2));
            m_CompanyPrefix = Common.BitsToPaddedInt(GetFieldBits(3), PartitionTable.GetCompanyPrefixLength(m_Partition));
            m_SerialReference = Common.BitsToPaddedInt(GetFieldBits(4), PartitionTable.GetSerialRefLength(m_Partition));
        }

        public int Filter
        {
            get
            {
                return m_Filter;
            }
        }

        public int Partition
        {
            get
            {
                return m_Partition;
            }
        }

        public string CompanyPrefix
        {
            get
            {
                return m_CompanyPrefix;
            }
        }

        public string SerialReference
        {
            get
            {
                return m_SerialReference;
            }
        }

        public string GetEANUCCSSCC() 
        {
            StringBuilder Result = new StringBuilder(17);
            Result.Append(SerialReference[0]);
            Result.Append(CompanyPrefix);
            for (int i = 1; i < SerialReference.Length; i++)
            {
                Result.Append(SerialReference[i]);
            }

            int Sum = Result[0] * 1;
            Sum += Result[1] * 3;
            Sum += Result[2] * 1;
            Sum += Result[3] * 3;
            Sum += Result[4] * 1;
            Sum += Result[5] * 3;
            Sum += Result[6] * 1;
            Sum += Result[7] * 3;
            Sum += Result[8] * 1;
            Sum += Result[9] * 3;
            Sum += Result[10] * 1;
            Sum += Result[11] * 3;
            Sum += Result[12] * 1;
            Sum += Result[13] * 3;
            Sum += Result[14] * 1;
            Sum += Result[15] * 3;
            Sum += Result[16] * 1;

            int D18 = (int)((Math.Ceiling(((double)Sum) / 10) * 10) - Sum);

            Result.Append(Math.Abs(D18));

            return Result.ToString();
        }

        public override int[] GetFieldSplits()
        {
            Bits data = GetBits();
            int[] Result = new int[5];
            Result[0] = 88;
            Result[1] = 85;
            Result[2] = 82;

            Bits PartitionBits = data.GetBits(82, 84);
            int Partition = (int)Common.BitsToInt(PartitionBits);
            int CoBitCount = PartitionTable.GetCompanyPrefixBitCount(Partition);

            Result[3] = 82 - CoBitCount;
            Result[4] = 24;

            return Result;
        }

        public override Uri GetTag()
        {
            StringBuilder Result = new StringBuilder();
            Result.Append("urn:epc:tag:");
            Result.Append(GetScheme());
            Result.Append(":");
            Result.Append(m_Filter);
            Result.Append(".");
            Result.Append(m_CompanyPrefix);
            Result.Append(".");
            Result.Append(m_SerialReference);

            return new Uri(Result.ToString());
        }

        public override Uri GetEPC()
        {
            StringBuilder Result = new StringBuilder();
            Result.Append("urn:epc:id:");
            Result.Append(GetSchemeName());
            Result.Append(":");
            Result.Append(m_Filter);
            Result.Append(".");
            Result.Append(m_CompanyPrefix);
            Result.Append(".");
            Result.Append(m_SerialReference);

            return new Uri(Result.ToString());
        }

        public override string GetScheme()
        {
            return GetSchemeName() + "-" + GetTagBitLength();
        }

        public override string GetSchemeName()
        {
            return "sscc";
        }

        private static class PartitionTable
        {
            public static int GetCompanyPrefixBitCount(int partition)
            {
                switch (partition)
                {
                    case 0:
                        return 40;
                    case 1:
                        return 37;
                    case 2:
                        return 34;
                    case 3:
                        return 30;
                    case 4:
                        return 27;
                    case 5:
                        return 24;
                    case 6:
                        return 20;
                    default:
                        throw new EPCParseException("Invalid Partition Code:" + partition);
                }
            }

            public static int GetCompanyPrefixLength(int partition)
            {
                if ((partition >= 0) && (partition <= 6))
                {
                    return 12 - partition;
                }

                throw new EPCParseException("Invalid Partition Code:" + partition);
            }

            public static int GetSerialRefBitCount(int partition)
            {
                switch (partition)
                {
                    case 0:
                        return 18;
                    case 1:
                        return 21;
                    case 2:
                        return 24;
                    case 3:
                        return 28;
                    case 4:
                        return 31;
                    case 5:
                        return 34;
                    case 6:
                        return 38;
                    default:
                        throw new EPCParseException("Invalid Partition Code:" + partition);
                }
            }

            public static int GetSerialRefLength(int partition)
            {
                if ((partition >= 0) && (partition <= 6))
                {
                    return 5 + partition;
                }

                throw new EPCParseException("Invalid Partition Code:" + partition);
            }

            public static int GetPartition(string companyPrefix)
            {
                int Length = companyPrefix.Length;

                if ((Length >= 6) && (Length <= 12))
                {
                    return 12 - Length;
                }

                throw new EPCParseException("Invalid CompanyPrefix " + companyPrefix);
            }
        }
    }
}
